package com.shangbang.verb.service.impl.record;

import com.shangbang.verb.dao.record.LikeMapper;
import com.shangbang.verb.dao.record.StoryMapper;
import com.shangbang.verb.entity.record.LikeEntity;
import com.shangbang.verb.entity.record.StoryEntity;
import com.shangbang.verb.service.record.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LikeServiceImpl implements LikeService {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private StoryMapper storyMapper;
    @Override
    public void putLikeInfo(Integer uid, Integer type, Integer story_id) {
        //0:点赞，1：取消
        LikeEntity likeEntity = likeMapper.getLikeInfo(uid,story_id);
        if (type==0){
            if (likeEntity==null){
                likeEntity = new LikeEntity();
                likeEntity.setStoryId(story_id);
                likeEntity.setUid(uid);
                likeMapper.insert(likeEntity);
            }
        }else if (type==1){
            if (likeEntity!=null){
                likeMapper.deleteById(likeEntity.getId());
            }
        }
    }

    @Override
    public Object getLikeList(Integer uid, Integer page, Integer num) {
        List<LikeEntity> entityList = likeMapper.getLikeList(uid,page,num);
        List<StoryEntity> storyEntityList = new ArrayList<StoryEntity>();

        if(entityList!=null&&entityList.size()>0){
            for (int i = 0; i <entityList.size() ; i++) {
                StoryEntity storyEntity = storyMapper.selectById(entityList.get(i).getStoryId());
                if (null!=storyEntity){
                    storyEntityList.add(storyEntity);
                }
            }
        }
        return storyEntityList;
    }
}